package com.eastseeway.radioproject.setting;

import android.annotation.SuppressLint;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.eastseeway.radioproject.R;
import com.eastseeway.radioproject.base.BaseActivity;
import com.eastseeway.radioproject.base.Const;
import com.eastseeway.radioproject.databinding.ActivitySettingBinding;
import com.eastseeway.radioproject.databinding.ItemSettingListBinding;
import com.eastseeway.radioproject.entities.MessageEvent;
import com.eastseeway.radioproject.entities.UserInfo;
import com.eastseeway.radioproject.views.DialogAddUser;
import com.eastseeway.radioproject.views.DialogInputPassword;
import com.eastseeway.radioproject.views.UnderlineDecoration;
import com.google.gson.Gson;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class SettingActivity extends BaseActivity<ActivitySettingBinding,SettingViewModel> {

    private SettingListAdapter mAdapter;

    @Override
    protected void initEvent() {
        mBinding.setViewmodel(mViewModel);

        initRv();

        mBinding.addUser.setOnClickListener(v->{
            DialogAddUser dialogAddUser = new DialogAddUser(this);
            dialogAddUser.show();
            dialogAddUser.setOnConfirmClickListener((username,password) -> {
                if (username.isEmpty() || password.isEmpty()) {
                    Toast.makeText(this,"请输入用户名或密码",Toast.LENGTH_SHORT).show();
                    return;
                }
                mViewModel.addUser(username,password);
                dialogAddUser.dismiss();
            });
        });

//        mBinding.deleteUser.setOnClickListener(v -> {
//            UserInfo userInfo = new UserInfo();
//            userInfo.setId(23);
//            mViewModel.deleteUser(userInfo);
//        });

        mBinding.back.setOnClickListener(v->{
            playClickSound();
            finish();
        });
    }

    private void initRv() {
        mAdapter = new SettingListAdapter();
        mBinding.settingUserList.setLayoutManager(new LinearLayoutManager(this));
        mBinding.settingUserList.setAdapter(mAdapter);
        mBinding.settingUserList.addItemDecoration(new UnderlineDecoration(this));
    }

    @Override
    protected void loadData() {
        mViewModel.queryAllUsers();
    }

    @Override
    protected void observeData() {
        super.observeData();
        mViewModel.getAllUsers().observe(this, userInfos -> {
            Log.e("12121","userInfos:"+new Gson().toJson(userInfos));
            mAdapter.setData(userInfos);
        });

        mViewModel.getToastInfo().observe(this, s -> {
            Toast.makeText(this,s,Toast.LENGTH_SHORT).show();
        });
    }

    @Override
    protected Class<SettingViewModel> getVMClass() {
        return SettingViewModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void OnEvent(MessageEvent messageEvent){
        if (messageEvent.getCode() == Const.TCP_CONNECT_STATUS) {
//            String msg = (String) messageEvent.getData();
//            Log.e("12121","SettingActivity OnEvent "+msg);
        }
    }

    private void showDialog(UserInfo userInfo){
        DialogInputPassword dialogInputPassword = new DialogInputPassword(this);
        dialogInputPassword.show();
        dialogInputPassword.setOnConfirmClickListener(password -> {
            if (password.isEmpty()) {
                Toast.makeText(this,"密码不能为空",Toast.LENGTH_SHORT).show();
                return;
            }
            userInfo.setPassword(password);
            mViewModel.updateUser(userInfo);
            dialogInputPassword.dismiss();
        });
    }

    class SettingListAdapter extends RecyclerView.Adapter<SettingListAdapter.InnerViewHolder>{

        List<UserInfo> mUserInfos = new ArrayList<>();
        @NonNull
        @Override
        public InnerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

            ItemSettingListBinding mBinding = ItemSettingListBinding.inflate(getLayoutInflater(),parent,false);
            return new InnerViewHolder(mBinding);
        }

        @Override
        public void onBindViewHolder(@NonNull InnerViewHolder holder, int position) {

            holder.mItemView.setUserInfo(mUserInfos.get(position));
            holder.mItemView.layoutSettingDelete.setOnClickListener(v -> {
                mViewModel.deleteUser(mUserInfos.get(position));
            });
            holder.mItemView.layoutSettingModify.setOnClickListener(v -> {
                showDialog(mUserInfos.get(position));
            });

//            UserInfo userInfo = mUserInfos.get(position);
//            holder.mItemView.layoutSettingName.setText(userInfo.getName());
//            holder.mItemView.layoutSettingDelete.setVisibility(userInfo.isIs_admin()? View.GONE:View.VISIBLE);
//            holder.mItemView.layoutSettingDelete.setOnClickListener(v -> {
//                if (!userInfo.isIs_admin()) {
//                    mViewModel.deleteUser(userInfo);
//                }
//            });
        }

        @Override
        public int getItemCount() {
            return mUserInfos.size();
        }

        class InnerViewHolder extends RecyclerView.ViewHolder{

            private final ItemSettingListBinding mItemView;

            public InnerViewHolder(@NonNull ItemSettingListBinding itemView) {
                super(itemView.getRoot());
                mItemView = itemView;
            }
        }

        @SuppressLint("NotifyDataSetChanged")
        public void setData(List<UserInfo> userInfos){
            mUserInfos.clear();
            mUserInfos.addAll(userInfos);
            notifyDataSetChanged();
        }
    }


}
